package com.sakura.回溯;

import java.util.ArrayList;
import java.util.List;

public class Code77_组合 {

    public static void main(String[] args) {
        Code77_组合 code77_组合 = new Code77_组合();
        List<List<Integer>> combine = code77_组合.combine(4, 2);
        System.out.println(combine);
    }

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> ans = new ArrayList<>();

    public List<List<Integer>> combine(int n, int k) {

        backTracking(n, k, 1);
        return res;
    }

    public void backTracking(int n, int k, int startIndex) {
        if (ans.size() == k) {
            res.add(new ArrayList<>(ans));
            return;
        }

        for (int i = startIndex; i <= n; i++) {
            ans.add(i);
            backTracking(n, k, i + 1);
            ans.remove(ans.size() - 1);
        }
    }
}
